package org.example.netty.httpserver.json;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JSONEchoServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof String) {
            String content = (String) msg;
            log.info("收到消息：{}", content);
            JSONData jsonData = JSONData.fromJson(content);
            jsonData.setFromServer(true);
            String backContent = jsonData.toJson();
            ctx.writeAndFlush(backContent);
            log.info("写回内容：{}", backContent);
        }
        else {
            super.channelRead(ctx, msg);
        }
    }
}
